<?php
class system_paginator_adapter_concept implements system_paginator_adapter_interface {
	protected $_limit;
	protected $_count;
	protected $_currentPageNumber;
	
	protected $_concept = null;
	protected $_where = array();
	protected $_order = array();
	
	public function __construct(system_concept_abstract $concept) {
		$this->_concept = $concept;
	}
	
	/**
	 * 设置列表数据的条件
	 * @param array|string $key
	 * @param string $val
	 * @return system_paginator_adapter_concept
	 */
	public function where($key, $val = null) {
		if(null == $val) {
			$this->_where[] = $key;
		} else {
			$this->_where[] = array($key => $val);
		}
		return $this;
	}
	
	/**
	 * 设置排序
	 * @param array|string $order
	 * @return system_paginator_adapter_concept
	 */
	public function order($order) {
		$this->_order[] = $order;
		return $this;
	}
	
	/**
	 * 得到当前条件的数据总数
	 * @return int
	 */
	public function total() {
		if(null == $this->_count) {
			$this->_count = $this->_concept->fetchCount($this->_where);
		}
		return $this->_count;
	}
	
	/**
	 * @return system_concept_rowset
	 */
	public function getItem() {
		return $this->_concept->fetchAll($this->_where, $this->_order, $this->_limit, $this->_currentPageNumber);
	}
	/**
	 * 得到总数
	 * @return int
	 */
	public function getTotal() {
		return $this->total();
	}
	
	/**
	 * 设置 总数
	 * @param int $count
	 * @return system_paginator_adapter_concept
	 */
	public function setTotal($count) {
		$this->_count;
		return $this;
	}
	/**
	 * 得到当前页标
	 * @return int
	 */
	public function getCurrentPageNumber() {
		return (int) $this->_currentPageNumber;
	}
	
	/**
	 * 设置 当前页标
	 * @param int $number
	 * @return system_paginator_adapter_concept
	 */
	public function setCurrentPageNumber($number) {
		return $this->_currentPageNumber = $number;
	}
	
	/**
	 * 设置每页显示的数据限制
	 * @param int $limit
	 * @return system_paginator_adapter_concept
	 */
	public function setLimit($limit) {
		$this->_limit = $limit;
		return $this;
	}
	public function getLimit() {
		if($this->_limit == null) {
			$this->setLimit($this->getTotal());
		}
		return (int) $this->_limit;
	}
}